home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / UTILS / GENGRAPH.ZIP / SPRSEGA.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-11  |  4.3 KB  |  181 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <ctype.h>
  5. #include <alloc.h>
  6. #include <dos.h>
  7.  
  8. #define TRUE 1
  9. #define FALSE 0
  10.  
  11. void EndIt(void);
  12.  
  13. FILE *handle;
  14. FILE *handle2;
  15. FILE *handle3;
  16. char filename1[13];
  17. char filename2[13];
  18. char filename3[13];
  19. char far *planeptr;
  20. char far *tmpptr;
  21. char far *bufferptr;
  22. int StartSprite;
  23. int SpriteValid;
  24. int Width;
  25. int Height;
  26. long filelength;
  27. struct spriteheader
  28. {
  29.     int  Version;
  30.     char SpriteFile[13];
  31.     char Palette[768];
  32.     int  NumSprites;
  33. };
  34.  
  35. spriteheader SpriteHeader;
  36.  
  37. int main(){
  38.  
  39.     clrscr();
  40.     printf("WGT Sprite to Sega Sprite converter.\n");
  41. EnterName:
  42.     printf("\nEnter the name of sprite file (include extension)> ");
  43.     scanf("%s",&filename1);
  44.     if (filename1=="")
  45.     {
  46.         printf("Please enter a sprite file name.\n");
  47.         goto  EnterName;
  48.     }
  49. EnterData:
  50.     printf("\nEnter the name of data file to save (include extension)> ");
  51.     scanf("%s",&filename2);
  52.  
  53.     if (filename2=="")
  54.     {
  55.         printf("Please enter a data file name.\n");
  56.         goto  EnterData;
  57.     }
  58.  
  59. EnterPal:
  60.     printf("\nEnter the name of palette file to save (include extension)> ");
  61.     scanf("%s",&filename3);
  62.  
  63.     if (filename3=="")
  64.     {
  65.         printf("Please enter a data file name.\n");
  66.         goto  EnterPal;
  67.     }
  68.  
  69.     handle=fopen(filename1,"rb");
  70.     if (handle==NULL)
  71.     {
  72.         printf("%s does not exist. Please enter another sprite file name.\n",filename1);
  73.         goto  EnterName;
  74.     }
  75.  
  76.     handle2=fopen(filename2,"wb");
  77.  
  78.     if (handle2==NULL)
  79.     {
  80.         printf("Could not create %s. Please enter another data file name.\n",filename2);
  81.         fclose(handle);
  82.         goto  EnterData;
  83.     }
  84.  
  85.     handle3=fopen(filename3,"wb");
  86.  
  87.     if (handle3==NULL)
  88.     {
  89.         printf("Could not create %s. Please enter another palette file name.\n",filename3);
  90.         fclose(handle);
  91.         fclose(handle2);
  92.         goto  EnterPal;
  93.     }
  94.  
  95.     planeptr=(char *)farmalloc(64000);
  96.     tmpptr=planeptr;
  97.  
  98.     bufferptr=(char *)farmalloc(300);
  99.  
  100.     fread(&SpriteHeader,sizeof(spriteheader),1,handle);
  101.  
  102.     for (int palloop = 0; palloop <16; palloop++)
  103.     {
  104.         unsigned char red   = (SpriteHeader.Palette[palloop * 3] + 2) >> 2;
  105.         unsigned char green = (SpriteHeader.Palette[palloop * 3 + 1] + 2) >> 2;
  106.         unsigned char blue  = (SpriteHeader.Palette[palloop * 3 + 2] + 2) >> 2;
  107.         unsigned short color =  blue | ((unsigned short)green << 12) | ((unsigned short)red << 8);
  108.         fwrite(&color, 1, 2, handle3);
  109.     }
  110.  
  111.     fclose(handle3);
  112.  
  113.     if (SpriteHeader.Version==4) StartSprite=0;
  114.     else StartSprite=1;
  115.  
  116.     fseek(handle,0,SEEK_END);
  117.     filelength=ftell(handle)-sizeof(spriteheader);
  118.     rewind(handle);
  119.  
  120.     if (filelength>65536)
  121.     {
  122.         printf("Invalid Sprite File.\n");
  123.         EndIt();
  124.     }
  125.  
  126.     fseek(handle, sizeof(spriteheader), SEEK_SET);
  127.     fread(tmpptr, 1, filelength, handle);
  128.     fclose(handle);
  129.  
  130.     for (int outerloop=StartSprite;outerloop<SpriteHeader.NumSprites+1;outerloop++)
  131.     {
  132.         SpriteValid=*tmpptr;
  133.         tmpptr+=2;
  134.         if (SpriteValid==1)
  135.         {
  136.             Width=*tmpptr;
  137.             tmpptr+=2;
  138.             Height=*tmpptr;
  139.             tmpptr+=2;
  140.             if ((Width==0) || (Height==0)) continue;
  141.  
  142.             for (int col = 0; col < (Width/8); col++)
  143.             {
  144.                 char far *ptr  = tmpptr + (col * 8);
  145.                 char far *bptr = bufferptr;
  146.  
  147.                 for (int row = 0; row < (Height/8); row++)
  148.                 {
  149.                     for (int line = 0; line < 8; line++)
  150.                     {
  151.                         for (int pixel = 0; pixel < 8; pixel += 2)
  152.                         {
  153.                             *bptr = (*(ptr++) & 15) << 4;
  154.                             *bptr |= *(ptr++) & 15;
  155.  
  156.                             bptr++;
  157.                         }
  158.                         ptr += (Width - 8);
  159.                     }
  160.                 }
  161.  
  162.                 fwrite(bufferptr, 1, (32 * Height / 8), handle2);
  163.             }
  164.             tmpptr += Width * Height;
  165.         }
  166.     }
  167.   fclose(handle2);
  168.  
  169.     EndIt();
  170.     return 1;
  171. }
  172.  
  173. void EndIt(void)
  174. {
  175.     fclose(handle);
  176.     fclose(handle2);
  177.     farfree(planeptr);
  178.     farfree(bufferptr);
  179.     exit(0);
  180. }
  181.